package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.project.domain.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.ConstructionContractMapper;
import com.hngates.project.domain.ConstructionContract;
import com.hngates.project.service.IConstructionContractService;
import com.hngates.common.core.text.Convert;

/**
 * 施工合同Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-22
 */
@Service
public class ConstructionContractServiceImpl implements IConstructionContractService {
    @Autowired
    private ConstructionContractMapper constructionContractMapper;

    /**
     * 查询施工合同
     *
     * @param id 施工合同主键
     * @return 施工合同
     */
    @Override
    public ConstructionContract selectConstructionContractById(Long id) {
        return constructionContractMapper.selectConstructionContractById(id);
    }

    /**
     * 查询施工合同列表
     *
     * @param constructionContract 施工合同
     * @return 施工合同
     */
    @Override
    public List<ConstructionContract> selectConstructionContractList(ConstructionContract constructionContract) {
        return constructionContractMapper.selectConstructionContractList(constructionContract);
    }

    /**
     * 新增施工合同
     *
     * @param constructionContract 施工合同
     * @return 结果
     */
    @Override
    public int insertConstructionContract(ConstructionContract constructionContract) {
        constructionContract.setCreateTime(DateUtils.getNowDate());
        return constructionContractMapper.insertConstructionContract(constructionContract);
    }

    /**
     * 修改施工合同
     *
     * @param constructionContract 施工合同
     * @return 结果
     */
    @Override
    public int updateConstructionContract(ConstructionContract constructionContract) {
        constructionContract.setUpdateTime(DateUtils.getNowDate());
        return constructionContractMapper.updateConstructionContract(constructionContract);
    }

    /**
     * 批量删除施工合同
     *
     * @param ids 需要删除的施工合同主键
     * @return 结果
     */
    @Override
    public int deleteConstructionContractByIds(String ids) {
        return constructionContractMapper.deleteConstructionContractByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除施工合同信息
     *
     * @param id 施工合同主键
     * @return 结果
     */
    @Override
    public int deleteConstructionContractById(Long id) {
        return constructionContractMapper.deleteConstructionContractById(id);
    }

    /**
     * @param constructionContractList 施工合同列表信息
     * @param updateSupport            是否支持更新 如果已经存在 则进行更新数据
     * @param operName                 操作用户
     * @return
     */
    @Override
    public String importConstructionContract(List<ConstructionContract> constructionContractList, Boolean updateSupport, String operName) {
        if (Objects.isNull(constructionContractList) || constructionContractList.size() == 0) {
            throw new BusinessException("导入施工合同不能为空");
        }
        StringBuilder importMsg = new StringBuilder();
        //循环excel中的记录
        for (ConstructionContract constructionContract : constructionContractList) {
            try {
                //constructionContract是循环中 当前的记录对象
                //根据当前的Excel中的对象去数据库中查 查询时 以Excel中名称作为参数去查 返回值是 与项目同名的记录转为数据库对象
                ConstructionContract constructionContract1 = constructionContractMapper.selectConstructionContractByConstructionId(constructionContract.getConstructionId());
                //判断数据库对象是否存在
                if (constructionContract1 == null) {
                    //如果不存在就创建
                    constructionContract1 = new ConstructionContract();
                    //给对象的字段名赋值
                    constructionContract1.setConstructionId(constructionContract.getConstructionId());
                    constructionContract1.setContractMoney(constructionContract.getContractMoney());
                    constructionContract1.setPaymentMethod(constructionContract.getPaymentMethod());
                    constructionContract1.setUnpaidBalance(constructionContract.getUnpaidBalance());
                    constructionContract1.setPaymentDate(constructionContract.getPaymentDate());

                    //赋值完成后 将这个对象插入到数据库中
                    constructionContractMapper.insertConstructionContract(constructionContract1);

                    importMsg.append("<br/>" + "施工合同" + constructionContract1.getConstructionId() + "导入成功");

                } else if (updateSupport) {
                    constructionContract1.setUpdateBy(operName);
                    constructionContract1.setContractMoney(constructionContract.getContractMoney());
                    constructionContract1.setPaymentMethod(constructionContract.getPaymentMethod());
                    constructionContract1.setUnpaidBalance(constructionContract.getUnpaidBalance());
                    constructionContract1.setPaymentDate(constructionContract.getPaymentDate());
                    constructionContractMapper.updateConstructionContract(constructionContract1);

                    importMsg.append("<br/>"  + "施工合同" + constructionContract1.getConstructionId() + "更新成功");

                } else {
                    importMsg.append("<br/>"  + "施工合同" + constructionContract1.getConstructionId() + "已存在");

                }

            } catch (Exception e) {
                    importMsg.append("<br/>"  + "施工合同" + constructionContract.getConstructionId() + "导入时异常");
            }
        }

        return importMsg.toString();
    }
}
